package com.ruoyi.erp.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.BusinessUnit;
import com.ruoyi.erp.service.IBusinessUnitService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 事业部Controller
 * 
 * @author sauger
 * @date 2022-03-14
 */
@Controller
@RequestMapping("/erp/businessUnit")
public class BusinessUnitController extends BaseController
{
    private String prefix = "erp/businessUnit";

    @Autowired
    private IBusinessUnitService businessUnitService;

    @RequiresPermissions("erp:businessUnit:view")
    @GetMapping()
    public String businessUnit()
    {
        return prefix + "/businessUnit";
    }

    /**
     * 查询事业部列表
     */
    @RequiresPermissions("erp:businessUnit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessUnit businessUnit)
    {
        startPage();
        List<BusinessUnit> list = businessUnitService.selectBusinessUnitList(businessUnit);
        return getDataTable(list);
    }

    /**
     * 导出事业部列表
     */
    @RequiresPermissions("erp:businessUnit:export")
    @Log(title = "事业部", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessUnit businessUnit)
    {
        List<BusinessUnit> list = businessUnitService.selectBusinessUnitList(businessUnit);
        ExcelUtil<BusinessUnit> util = new ExcelUtil<BusinessUnit>(BusinessUnit.class);
        return util.exportExcel(list, "事业部数据");
    }

    /**
     * 新增事业部
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存事业部
     */
    @RequiresPermissions("erp:businessUnit:add")
    @Log(title = "事业部", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusinessUnit businessUnit)
    {
        businessUnit.setCreateBy(this.getLoginName());
        businessUnit.setUpdateBy(this.getLoginName());
        businessUnit.setUpdateTime(DateUtils.getNowDate());
        return toAjax(businessUnitService.insertBusinessUnit(businessUnit));
    }

    /**
     * 修改事业部
     */
    @RequiresPermissions("erp:businessUnit:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        BusinessUnit businessUnit = businessUnitService.selectBusinessUnitById(id);
        mmap.put("businessUnit", businessUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存事业部
     */
    @RequiresPermissions("erp:businessUnit:edit")
    @Log(title = "事业部", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusinessUnit businessUnit)
    {
        businessUnit.setUpdateBy(this.getLoginName());
        return toAjax(businessUnitService.updateBusinessUnit(businessUnit));
    }

    /**
     * 删除事业部
     */
    @RequiresPermissions("erp:businessUnit:remove")
    @Log(title = "事业部", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(businessUnitService.deleteBusinessUnitByIds(ids));
    }
}
